jQuery.noConflict();

(function($, PLUGIN_ID) {

    "use strict";

    $(document).ready(function() {

        var kintonePluginMustWithConditions = {

            CONTAIN: "CONTAINED",
            NOT_CONTAIN: "NOT CONTAINED",
            EQUAL: "=",
            NOT_EQUAL: "≠",
            GREATE_THEN: ">",
            LESS_THEN: "<",
            GREATE_THEN_EQUAL: "≧",
            LESS_THEN_EQUAL: "≦",
            
            CONF: kintone.plugin.app.getConfig(PLUGIN_ID),
            CONF2: kintone.plugin.app.getConfig(PLUGIN_ID).condition,
            CONFIG: JSON.parse(kintone.plugin.app.getConfig(PLUGIN_ID).condition),
    
            EVENT:["app.record.edit.submit","app.record.create.submit","app.record.index.edit.submit"],
              


//比较所填的值和所取的值            
            checkNumber: function(value) {

                if (isFinite(value)) {
                    return true;
                }

                return false;
            },
            
            checkUndefined: function(value) {

                if (value === undefined || value.length === 0 || value === "<div><br></div>") {
                    return "" ;
                }

                return value; 

                },  


            checkValue: function(conf, thisValue) {

                var express = conf.conditionalExpression;//第二列表达式的值
                var conditionValue = conf.conditionalValue;//第三列条件值[]                   
                var value = this.checkUndefined(thisValue);//×××××实际输入的值
                var isNum = this.checkNumber(conditionValue) && this.checkNumber(value);//条件值和实际输入的值是否都是数字
                var h;

                switch (express) {
                    case this.CONTAIN:

                        for (h = 0; h < conditionValue.length; h++) {
                            if (value.match(conditionValue[h])) {
                                return true;
                            } 
                            continue;
                        }
                        
                        return false;

                       

                    case this.NOT_CONTAIN:
                        

                        for (h = 0; h < conditionValue.length; h++) {
                            if (value.match(conditionValue[h])) {
                                return false;
                            } 
                            continue;
                        }

                        return true;

                    case this.EQUAL:

                        if (isNum) {
                            if (parseInt(value) === parseInt(conditionValue)) {
                                return true;
                            }
                        }

                        if (value === conditionValue) { //即使不是数字两者的值一样也返回true
                            return true;
                        }

                        return false;

                    case this.NOT_EQUAL:

                        if (isNum) {
                            if (parseInt(value) === parseInt(conditionValue)) {
                                return false;
                            }
                        }

                        if (value === conditionValue) {//即使不是数字两者的值一样返回fause
                            return false;
                        }

                        return true;

                    case this.GREATE_THEN:

                        if (!(isNum)) {
                            return false;
                        }

                        if (parseInt(value) > parseInt(conditionValue)) {
                            return true;
                        }

                        return false;

                    case this.LESS_THEN:

                        if (!(isNum)) {
                            return false;
                        }

                        if (parseInt(value) < parseInt(conditionValue)) {
                            return true;
                        }

                        return false;

                    case this.GREATE_THEN_EQUAL:

                        if (!(isNum)) {
                            return false;
                        }

                        if (parseInt(value) >= parseInt(conditionValue)) {
                            return true;
                        }

                        return false;

                    case this.LESS_THEN_EQUAL:

                        if (!(isNum)) {
                            return false;
                        }

                        if (parseInt(value) <= parseInt(conditionValue)) {
                            return true;
                        }

                        return false;
                }
            },


            checkType: function(conf, field) {

                var i;

                switch (field.type) {
                    case "CHECK_BOX":
                    case "MULTI_SELECT":

                        for (i = 0; i <= field.value.length; i++) {//如果数组长度为0，0<0就不进去了
                            if (this.checkValue(conf, field.value[i])) {//在这种情况要检查他们的值，满足就返回true
                                return true;
                            }
                        }
                        return false;

                    default:
                        return this.checkValue(conf, field.value);//其他默认的情况检查他们的值
                }
            },


            checkRecords: function(record) {

                for (var i = 0; i < this.CONFIG.length; i++) {

                    var conf = this.CONFIG[i];//获取插件设置的内容
                    console.log(conf);

                    for (var key in record) { //查找APP中是否有这个字段代码

                        if (!(record.hasOwnProperty(key))) {
                            continue;
                        }

                        if (conf.selectField !== key) {
                            continue;
                        }
                          
                        
                    conf.isMust = this.checkType(conf, record[key]); 
                        
                        //找到之后判断这个字段代码的类型  
                    }
                }
            },

            checkConditionFormat: function(record) {
                
                
                this.checkRecords(record);

                var mustFields = [];
                
              
                for (var i = 0; i < this.CONFIG.length; i++) {

                    var conf = this.CONFIG[i];
                    
                    if (!(conf.isMust)) {
                        continue;
                    }
                    
                    
                    for (var j = 0; j < conf.mustField.length; j++) {

                        var fieldName = conf.mustField[j];

                        if (mustFields.indexOf(fieldName) === -1) {
                            mustFields.push(fieldName);
                        }

                        if (conf.isMust) {
                            if (record[fieldName]["value"] === undefined || record[fieldName]["value"].length === 0 || record[fieldName]["value"] === "<div><br></div>") {
                            record[fieldName]["error"]="此为必填项"; 
                            }
                            
                        }
                    }
                
                } 
            },        
            
            

            setEvent: function() {

                var self = this;

                kintone.events.on(this.EVENT, function(event) { 

                    self.checkConditionFormat(event.record);

                    return event;
                });
            },

            init: function() {

                this.setEvent();
            }
        };
        kintonePluginMustWithConditions.init();
    });


})(jQuery, kintone.$PLUGIN_ID);
